package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.IsurerService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.IsurerDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing Isurer.
 */
@RestController
@RequestMapping("/api")
public class IsurerResource {

    private final Logger log = LoggerFactory.getLogger(IsurerResource.class);

    private static final String ENTITY_NAME = "isurer";

    private final IsurerService isurerService;

    public IsurerResource(IsurerService isurerService) {
        this.isurerService = isurerService;
    }

    /**
     * POST  /isurers : Create a new isurer.
     *
     * @param isurerDTO the isurerDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new isurerDTO, or with status 400 (Bad Request) if the isurer has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/isurers")
    @Timed
    public ResponseEntity<IsurerDTO> createIsurer(@RequestBody IsurerDTO isurerDTO) throws URISyntaxException {
        log.debug("REST request to save Isurer : {}", isurerDTO);
        if (isurerDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new isurer cannot already have an ID")).body(null);
        }
        IsurerDTO result = isurerService.save(isurerDTO);
        return ResponseEntity.created(new URI("/api/isurers/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /isurers : Updates an existing isurer.
     *
     * @param isurerDTO the isurerDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated isurerDTO,
     * or with status 400 (Bad Request) if the isurerDTO is not valid,
     * or with status 500 (Internal Server Error) if the isurerDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/isurers")
    @Timed
    public ResponseEntity<IsurerDTO> updateIsurer(@RequestBody IsurerDTO isurerDTO) throws URISyntaxException {
        log.debug("REST request to update Isurer : {}", isurerDTO);
        if (isurerDTO.getId() == null) {
            return createIsurer(isurerDTO);
        }
        IsurerDTO result = isurerService.save(isurerDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, isurerDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /isurers : get all the isurers.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of isurers in body
     */
    @GetMapping("/isurers")
    @Timed
    public ResponseEntity<List<IsurerDTO>> getAllIsurers(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of Isurers");
        Page<IsurerDTO> page = isurerService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/isurers");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /isurers/:id : get the "id" isurer.
     *
     * @param id the id of the isurerDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the isurerDTO, or with status 404 (Not Found)
     */
    @GetMapping("/isurers/{id}")
    @Timed
    public ResponseEntity<IsurerDTO> getIsurer(@PathVariable Long id) {
        log.debug("REST request to get Isurer : {}", id);
        IsurerDTO isurerDTO = isurerService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(isurerDTO));
    }

    /**
     * DELETE  /isurers/:id : delete the "id" isurer.
     *
     * @param id the id of the isurerDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/isurers/{id}")
    @Timed
    public ResponseEntity<Void> deleteIsurer(@PathVariable Long id) {
        log.debug("REST request to delete Isurer : {}", id);
        isurerService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/isurers?query=:query : search for the isurer corresponding
     * to the query.
     *
     * @param query the query of the isurer search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/isurers")
    @Timed
    public ResponseEntity<List<IsurerDTO>> searchIsurers(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of Isurers for query {}", query);
        Page<IsurerDTO> page = isurerService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/isurers");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
